
// /找至多包含两种元素的最长子串，返回其长度

public class Solution {
    public int TotalFruit(int[] fruits) {
        int n = fruits.Length;
        IDictionary<int, int> cnt = new Dictionary<int, int>();

        int left = 0, ans = 0;
        // 滑动创建
        for (int right = 0; right < n; ++right) {
            cnt.TryAdd(fruits[right], 0);
            ++cnt[fruits[right]];
            while (cnt.Count > 2) {
                --cnt[fruits[left]];
                if (cnt[fruits[left]] == 0) {
                    cnt.Remove(fruits[left]);
                }
                ++left;
            }
            ans = Math.Max(ans, right - left + 1);
        }
        return ans;
    }
}

